package com.mxwl.cbg.common.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class TimeUtils {
    // 定义日期时间格式
    private static final DateTimeFormatter FORMATTER_WITH_MILLIS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
    private static final DateTimeFormatter FORMATTER_WITHOUT_MILLIS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final ZoneId TARGET_TIMEZONE = ZoneId.of("Asia/Shanghai");

    public static ZonedDateTime parseDateTimeWithTimeZone(String dateTimeStr) {
        // 尝试使用包含毫秒的格式解析
        try {
            return ZonedDateTime.of(LocalDateTime.parse(dateTimeStr, FORMATTER_WITH_MILLIS), TARGET_TIMEZONE);
        } catch (DateTimeParseException e) {
            // 如果解析失败，尝试使用不包含毫秒的格式解析
            return ZonedDateTime.of(LocalDateTime.parse(dateTimeStr, FORMATTER_WITHOUT_MILLIS), TARGET_TIMEZONE);
        }
    }
}
